//
//  FDCaseMangerController.m
//  FamousDoctorOnline
//
//  Created by wszyxc on 16/7/5.
//  Copyright © 2016年 wszyxc. All rights reserved.
//

#import "FDCaseMangerController.h"
#import "FDCaseModel.h"
#import "FDCaseCell.h"
#import "FDAddCaseController.h"

@interface FDCaseMangerController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)UIView *topView;
@property (nonatomic,strong)UIImageView *userImage;
@property (nonatomic,strong)UILabel *nameLabel;
@property (nonatomic,strong)UILabel *label1;
@property (nonatomic,strong)UILabel *label2;
@property (nonatomic,strong)UILabel *label3;
@property (nonatomic,strong)UILabel *label4;
@property (nonatomic,strong)UITableView *caseManager;

@property (nonatomic,strong)UIButton *addButton;

@property (nonatomic,strong)NSMutableArray<FDCaseModel *> *caseArray;

@end

@implementation FDCaseMangerController

-(NSMutableArray<FDCaseModel *> *)caseArray
{
    if(!_caseArray)
    {
        _caseArray = [NSKeyedUnarchiver unarchiveObjectWithFile:[KDocumentFilePath stringByAppendingPathComponent:@"data.data"]];
        if(_caseArray == nil)
        {
        
        _caseArray = [NSMutableArray array];
        }
    }
    return _caseArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupTopView];
    
    [self setupTableView];
}

-(void)setupTopView
{
    UIView *topView = [[UIView alloc]init];
    
    self.topView = topView;
//    topView.backgroundColor = [UIColor redColor];
    
    [self.view addSubview:topView];
    
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.top.mas_equalTo(64);
        make.width.mas_equalTo(SCREEN_WIDTH);
        make.height.mas_equalTo(180);
    }];
    
    UIImageView *userImage = [[UIImageView alloc]init];
    self.userImage = userImage;
    userImage.backgroundColor = [UIColor blueColor];
    [self.topView addSubview:userImage];
    
    [userImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.top.equalTo(self.topView.mas_top).offset(20);
        make.height.width.mas_equalTo(100);
    }];
    
    UILabel *nameLabel = [[UILabel alloc]init];
    self.nameLabel  = nameLabel;
    [self.topView addSubview:nameLabel];
    nameLabel.text = @"王二狗";
    [nameLabel sizeToFit];
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.userImage.mas_bottom).offset(10);
        make.centerX.equalTo(self.userImage.mas_centerX);
        
    }];
    
    UIImageView *img1 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"0"]];
    [self.topView addSubview:img1];
    [img1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.userImage.mas_right).offset(20);
        make.top.equalTo(self.userImage.mas_top);
        make.height.width.mas_equalTo(16);
    }];
    
    UILabel *label1 = [[UILabel alloc]init];
    label1.text = @"56416531";
    self.label1 = label1;
    [self.topView addSubview:label1];
    [label1 sizeToFit];
    [label1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(img1.mas_top);
        make.left.equalTo(self.userImage.mas_right).offset(45);
    }];
    
    UIImageView *img2 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"idCard"]];
    [self.topView addSubview:img2];
    [img2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(img1.mas_left);
        make.top.mas_equalTo(46);
        make.height.width.mas_equalTo(img1.mas_width);
        
        
    }];
    
    UILabel *label2 = [[UILabel alloc]init];
    label2.text = @"56416531";
    self.label2 = label2;
    [self.topView addSubview:label2];
    [label2 sizeToFit];
    [label2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(46);
        make.left.equalTo(img2.mas_right).offset(10);
    }];
    
    UIImageView *img3 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"phone"]];

    [self.topView addSubview:img3];
    [img3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(img2.mas_left);
        make.top.equalTo(img2.mas_bottom).offset(10);
        make.height.width.mas_equalTo(img1.mas_width);

    }];

    UILabel *label3 = [[UILabel alloc]init];
    label3.text = @"56416531";
    self.label3 = label3;
    [self.topView addSubview:label3];
    [label3 sizeToFit];
    [label3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(img3.mas_top);
        make.left.equalTo(img3.mas_right).offset(10);
    }];
    
    UIImageView *img4 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"illness_rb_img_sel.png"]];
    [self.topView addSubview:img4];
    [img4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(img3.mas_left);
        make.top.equalTo(img3.mas_bottom).offset(10);
        make.height.width.mas_equalTo(img1.mas_width);

    }];

    UILabel *label4 = [[UILabel alloc]init];
    label4.text = @"56416531";
    self.label4 = label4;
    [self.topView addSubview:label4];
    [label4 sizeToFit];
    [label4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(img4.mas_top);
        make.left.equalTo(img4.mas_right).offset(10);
    }];
    
    UIImageView *lineView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"fengexian"]];
    [self.topView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.bottom.equalTo(self.topView.mas_bottom);
        make.left.equalTo(self.topView.mas_left).offset(10);
        make.right.equalTo(self.topView.mas_right).offset(-10);
        make.height.mas_equalTo(1);
    }];
    
    UIButton *addbutton = [[UIButton alloc]init];
    self.addButton = addbutton;
    [addbutton setBackgroundColor:FDColor(34, 183, 196)];
    [addbutton setTitle:@"添加病例" forState:UIControlStateNormal];
    addbutton.titleLabel.textColor = [UIColor whiteColor];
    addbutton.titleLabel.font = [UIFont systemFontOfSize:18];
    [addbutton addTarget:self action:@selector(clickAddCaseButton) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:addbutton];
    [addbutton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(self.topView.mas_centerX);
        make.height.mas_equalTo(40);
        make.bottom.mas_equalTo(-10);
        make.width.mas_equalTo(300);
        
    }];
    
    

}

-(void)setupTableView{
    
    UITableView *caseManager = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
    
    self.caseManager = caseManager;
    self.caseManager.dataSource = self;
    self.caseManager.delegate  = self;
    
    caseManager.rowHeight = 175;
    
    caseManager.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.caseManager registerClass:[FDCaseCell class] forCellReuseIdentifier:@"caseManager"];
    [self.view addSubview:caseManager];
    
    [caseManager mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topView.mas_bottom);
        make.bottom.mas_equalTo(-70);
        make.left.right.mas_equalTo(0);
    }];
    
    caseManager.tableHeaderView.height = 0;
}


#pragma mark - tableview的数据源方法

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.caseArray.count;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FDCaseCell *cell = [tableView dequeueReusableCellWithIdentifier:@"caseManage"];
    if(!cell)
    {
        cell = [[FDCaseCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"caseManage"];
    }
    
    cell.model = self.caseArray[indexPath.row];
    
    return cell;
}

-(void)clickAddCaseButton
{
    FDAddCaseController *vc = [[FDAddCaseController alloc]init];
    vc.modelBlock = ^(FDCaseModel *model)
    {
        [self.caseArray insertObject:model atIndex:0];
        
        
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            
            [NSKeyedArchiver archiveRootObject:self.caseArray toFile:[KDocumentFilePath stringByAppendingPathComponent:@"data.data"]];
            
        });
        
        
        [self.caseManager reloadData];
        
    };
    
    [self.navigationController pushViewController:vc animated:YES];
}

@end
